package com.sisy.dubbo_consumer;

import com.alibaba.fastjson.JSONObject;
import com.sisy.common.RefundOrder;
import com.sisy.common.User;
import com.sisy.common.UserService;
import com.sisy.service.RefundService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@SpringBootApplication
@Controller
@ComponentScan({"com.sisy"})
@ImportResource({
        "classpath:spring/*.xml"
})
@MapperScan(basePackages = "com.sisy.mapper")
public class DubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

    @Autowired
    private UserService userService;

    @GetMapping()
    @ResponseBody
    public String hello(){
        return "HELLO DUBBO_CONSUMER";
    }

    @GetMapping("/user")
    @ResponseBody
    public User getUser(){
        User u = userService.getUser();
        return u;
    }

    @Autowired
    private RefundService refundService;

    @PostMapping("/refund")
    @ResponseBody
    public String refund(@RequestBody String payload){
        JSONObject json = JSONObject.parseObject(payload);
        refundService.refund(json.getInteger("id"), new BigDecimal(json.getString("refund")));
        return "SUCCESS";
    }

    @GetMapping("/refund/{id}")
    @ResponseBody
    public RefundOrder getRefundOrder(@PathVariable int id){
        RefundOrder refundOrder = refundService.getRefundOrderById(id);
        return refundOrder;
}
}
